using System;
using System.Collections.Generic;
using System.Text;
using RapidHDL;



namespace NegBinary
{
    public class NegBinaryPadFrame : PadFrame 
    {
        const int WIDTH = 4;
        NegBinaryMultiply oMultiplier;

        public override bool ConfigureSettings()
        {
            Settings.UseStart = false;
            Settings.BuildHardware = true;
            Settings.RunFromXilinx = true;
            Settings.ClockFrequency = 1;
            Settings.UseSDRAM = false;
            Settings.UseReset = false;
            Settings.BuildRemote = true;
            Settings.SynthASIC = true;
            Settings.SimulateVerilog = true;
            Settings.ForceBuildHardware = true;
            Settings.BuildServerPath = "desktop:8080";
            return true;
        }

        public override bool GenerateStructure(TopLevelComponent TopLevelComponent)
        {


            oMultiplier = new NegBinaryMultiply(TopLevelComponent, "NegaMult", WIDTH);

            SignalSource ssInA = SystemInterface.AddSignalSource("I_MUL_A",oMultiplier.InputANodes,new string('0',WIDTH));
            SignalSource ssInB = SystemInterface.AddSignalSource("I_MUL_B",oMultiplier.InputBNodes,new string('0',WIDTH));

            AssignPinCLK(39);
            AssignPinGND(40);
            AssignPinVDD(20);
            AssignPinInput("I_MUL_A", 10);
            AssignPinInput("I_MUL_B", 14);
            return true;
        }

        public override bool ConfigureSignalViews(TopLevelComponent TopLevelComponent)
        {
            SystemInterface.AddSignalView("O_MUL", "NegaMult.Out");
            AssignPinOutput("O_MUL", 21);
            return true;
        }

        private void ShowOutputs(HardwareScript poScript, int piTime)
        {
            poScript.SignalView(piTime, "O_MUL");
        }

        private void DoTest(HardwareScript poScript, int piTime, long piA, long piB)
        {
            poScript.SignalOut(piTime, "I_MUL_A", Convert.LongToNegBinary(piA,WIDTH));
            poScript.SignalOut(piTime, "I_MUL_B", Convert.LongToNegBinary(piB, WIDTH));
            poScript.SignalCheck(piTime + 1, "O_MUL", Convert.LongToNegBinary(piA * piB, WIDTH * 2 + 1));
        }
        public override bool GenerateHardwareScripts()
        {
            HardwareScript oScript = HardwareScripts.NewScript("test");
            int piTime = 1;

            DoTest(oScript, piTime+=2, 5, -7);
            DoTest(oScript, piTime+=2, oMultiplier.LowRange, oMultiplier.LowRange);
            DoTest(oScript, piTime += 2, oMultiplier.HighRange, oMultiplier.LowRange);
            DoTest(oScript, piTime += 2, oMultiplier.LowRange, oMultiplier.HighRange);
            DoTest(oScript, piTime += 2, oMultiplier.HighRange, oMultiplier.HighRange);
            DoTest(oScript, piTime += 2, oMultiplier.HighRange, -7);
            DoTest(oScript, piTime += 2, oMultiplier.LowRange, 2);
            DoTest(oScript, piTime += 2, oMultiplier.HighRange, -3);
            DoTest(oScript, piTime += 2, 0, -3);
            return true;
        }

    }
}
